package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import biz.MissionBiz;
import biz.TeamManagerBiz;
import biz.UserBiz;
import pojo.Users;
import util.DateTransfrom;

/**
 * @author WuYuan
 * 
 */
@Controller
public class UsersController {
	@Resource(name="userBiz")
	private UserBiz userBiz;
	
	@Resource(name="teamManagerBiz")
	private TeamManagerBiz teamManagerBiz;
	
	@Resource(name="missionBiz")
	private MissionBiz missionBiz;

	/*public TeamManagerBiz getTeamManagerBiz() {
		return teamManagerBiz;
	}

	public void setTeamManagerBiz(TeamManagerBiz teamManagerBiz) {
		this.teamManagerBiz = teamManagerBiz;
	}*/

	@RequestMapping("login.do")
	public String userLogin(Users u, HttpSession session,HttpServletRequest request) {
		System.out.println(u.getUserName()+u.getPassword());
		Users currentUser = userBiz.login(u);
		if (currentUser != null) {
			session.setAttribute("currentUser", currentUser);
			List<Map<String, Object>> tlist;
			try {
				tlist = teamManagerBiz.gotoTeamList(currentUser.getUserId());
				request.setAttribute("tlist", tlist);
				return "teamlist";
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}else {
			return "index";
		}
		return null;
	}

	@RequestMapping("logout.do")
	public String userLogout(HttpSession session,HttpServletRequest request) {
		Users currentUser = (Users) session.getAttribute("currentUser");
		System.out.println("当前session用户是"+currentUser.getUserName());
		session.removeAttribute("currentUser");
		return "index";
	}
	
	@RequestMapping("register.do")
	public String userRegister(Users u) {
		System.out.println(u.toString());
		if(userBiz.addregister(u)==1){
			return "registersuccess";
		}
		return "registersuccess";
	}
	
	@RequestMapping("gotoProjectOrMission.do")
	public String gotoProjectOrMission(String projectId,HttpSession session,HttpServletRequest request) {
		System.out.println("项目任务跳转测试:projectid="+projectId);
		Users currentUser = (Users) session.getAttribute("currentUser");
		System.out.println("当前用户在该项目里的角色是"+missionBiz.findRoleidByUrpid(projectId, String.valueOf(currentUser.getUserId())).getRoleId());
		if((missionBiz.findRoleidByUrpid(projectId, String.valueOf(currentUser.getUserId())).getRoleId())==2){
			return "redirect:/gotoxmjl.do?projectId=" + projectId+"&roleId=2";
		}else if((missionBiz.findRoleidByUrpid(projectId, String.valueOf(currentUser.getUserId())).getRoleId())==3){
			return "redirect:/findMissionsByUrpid.do?projectId=" + projectId+"&userId="+currentUser.getUserId()+"&roleId=3";
		}else
		return "registersuccess";
	}
	
	@RequestMapping("goto.do")
	public String gotoTeammember(String teamid,HttpSession session, HttpServletRequest request) {
		Users currentUser = (Users) session.getAttribute("currentUser");
		System.out.println("去往子模块的当前用户是"+String.valueOf(currentUser.getUserId())+" teamid是"+teamid);
		if(userBiz.judgeUserAndTeam( String.valueOf(currentUser.getUserId()),teamid) != null){
			System.out.println("使用者是团队创建者");
			return "redirect:/gotoTeamDetail.do?teamid=" + teamid;
		}else{
			List<Map<String, Object>> teamlist;
			try {
				teamlist = teamManagerBiz.gotoTeamList(currentUser.getUserId());
				request.setAttribute("teamlist", teamlist);
				return "teammember";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		/*List<Teams> memberTeamlist = missionBiz.findTeamsByUrpid( String.valueOf(currentUser.getUserId()), roleid);
		if(memberTeamlist.isEmpty()){
			System.out.println("没查到teamlist");
		}else{
			for(int i=0;i<memberTeamlist.size();i++){
			System.out.println("teamlist第"+i+"条是"+memberTeamlist.get(i).getTeamName());
			}
		}
		request.setAttribute("memberTeamlist", memberTeamlist);
		request.setAttribute("memberTeamid", teamid);*/
		return "teammember";
	}
	
	@ResponseBody
	@RequestMapping("validateUsername.do")
	public void userValidater(String userName, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out= null;
		Users validateUser = userBiz.validateUser(userName);
		try {
			out=response.getWriter();
			if(validateUser != null){
				out.print("true");
			}else out.println("false");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
	}
	
	@RequestMapping("lookdate.do")
	public void lookdate(String date) {
		System.out.println(DateTransfrom.transDateToStr(DateTransfrom.transStrToDate(DateTransfrom.spliteStrForDate(date))));
	}
	
	@RequestMapping("updatepmUsers.do")
	public String updatepmUsers(String userName, HttpSession session) throws Exception {
		Users currentUser = (Users) session.getAttribute("currentUser");
		if(userName!=null&&!userName.equals("")){
		System.out.println(userName);
		Users users = new Users();
		Date createDate = new Date();
		// int userId=333;
		users.setUserId(currentUser.getUserId());
		users.setUserName(userName);
		// users.setCreateDate(createDate);
		// users.setEmail("1212");
		// users.setPassword("13");
		// users.setTel("10");
		teamManagerBiz.updateUsers(users);
		return "updateinfosuccess";
		}else{
			return "pmuserinfo";
		}
	}
}
